#include "stdafx.h" 
#include "NADroptarget.h"


CNADroptarget::CNADroptarget(CNADroptargeter* lpDroptargeter)
:m_lpDroptargeter(lpDroptargeter)
,m_piDropHelper(NULL)
,m_bUseDnDHelper(false)
{
	if ( SUCCEEDED( CoCreateInstance ( CLSID_DragDropHelper, NULL, 
		CLSCTX_INPROC_SERVER,
		IID_IDropTargetHelper, 
		(void**) &m_piDropHelper ) ))
	{
		m_bUseDnDHelper = true;
	}
}

CNADroptarget::~CNADroptarget(){}     

//
// OnDragEnter is called by OLE dll's when drag cursor enters
// a window that is REGISTERed with the OLE dll's
//
DROPEFFECT CNADroptarget::OnDragEnter(
									  CWnd* pWnd,
									  COleDataObject* pDataObject,
									  DWORD dwKeyState,
									  CPoint point )
{
	DROPEFFECT dwEffect = DROPEFFECT_NONE;
	if( m_lpDroptargeter ) {
		dwEffect = m_lpDroptargeter->OnDragEnter(pWnd,pDataObject,dwKeyState,point);
	}else{
		dwEffect =  COleDropTarget::OnDragEnter(pWnd,pDataObject,dwKeyState,point);
	}

	if ( m_bUseDnDHelper )
	{
		// The DnD helper needs an IDataObject interface, so get one from
		// the COleDataObject.  Note that the FALSE param means that
		// GetIDataObject will not AddRef() the returned interface, so 
		// we do not Release() it.

		IDataObject* piDataObj = pDataObject->GetIDataObject ( FALSE ); 

		m_piDropHelper->DragEnter ( pWnd->GetSafeHwnd(), piDataObj, 
			&point, dwEffect );
	}

	return dwEffect;
}

// 
// OnDragOver is called by OLE dll's when cursor is dragged over 
// a window that is REGISTERed with the OLE dll's
//
DROPEFFECT CNADroptarget::OnDragOver(
									 CWnd* pWnd,
									 COleDataObject* pDataObject,
									 DWORD dwKeyState,
									 CPoint point )
{
	DROPEFFECT dwEffect = DROPEFFECT_NONE;
	if( m_lpDroptargeter ) {
		dwEffect =  m_lpDroptargeter->OnDragOver(pWnd,pDataObject,dwKeyState,point);
	}
	else
	{
		dwEffect =  COleDropTarget::OnDragOver(pWnd,pDataObject,dwKeyState,point);
	}
	

	if ( m_bUseDnDHelper )
	{
		m_piDropHelper->DragOver ( &point, dwEffect );
	}

	return dwEffect;
}

// 
// OnDrop is called by OLE dll's when item is dropped in a window
// that is REGISTERed with the OLE dll's
//
BOOL CNADroptarget::OnDrop(
						   CWnd* pWnd,
						   COleDataObject* pDataObject,
						   DROPEFFECT dropEffect,
						   CPoint point )
{
	DROPEFFECT dwEffect = DROPEFFECT_NONE;
	if( m_lpDroptargeter ) {
		dwEffect =  m_lpDroptargeter->OnDrop(pWnd,pDataObject,dropEffect,point);
	}else{
		dwEffect =  COleDropTarget::OnDrop(pWnd,pDataObject,dropEffect,point);
	}

	if ( m_bUseDnDHelper )
	{
		// The DnD helper needs an IDataObject interface, so get one from
		// the COleDataObject.  Note that the FALSE param means that
		// GetIDataObject will not AddRef() the returned interface, so 
		// we do not Release() it.

		IDataObject* piDataObj = pDataObject->GetIDataObject ( FALSE ); 

		m_piDropHelper->Drop ( piDataObj, &point, dwEffect );
	}

	return dwEffect;
}

DROPEFFECT CNADroptarget::OnDropEx(
								   CWnd* pWnd, 
								   COleDataObject* pDataObject,
								   DROPEFFECT dropDefault,
								   DROPEFFECT dropList,
								   CPoint point)
{
	DROPEFFECT dwEffect = DROPEFFECT_NONE;
	if( m_lpDroptargeter ) {
		dwEffect =  m_lpDroptargeter->OnDropEx(pWnd,pDataObject,dropDefault,dropList,point);
	}else{
		dwEffect =  COleDropTarget::OnDropEx(pWnd,pDataObject,dropDefault,dropList,point);
	}

	if ( m_bUseDnDHelper )
	{
		// The DnD helper needs an IDataObject interface, so get one from
		// the COleDataObject.  Note that the FALSE param means that
		// GetIDataObject will not AddRef() the returned interface, so 
		// we do not Release() it.

		IDataObject* piDataObj = pDataObject->GetIDataObject ( FALSE ); 

		m_piDropHelper->Drop ( piDataObj, &point, dwEffect );
	}

	return dwEffect;
}

//
// OnDragLeave is called by OLE dll's when drag cursor leaves
// a window that is REGISTERed with the OLE dll's
//
void CNADroptarget::OnDragLeave(CWnd* pWnd)
{
	if( m_lpDroptargeter )
		m_lpDroptargeter->OnDragLeave(pWnd);

	// Call base class implementation
	COleDropTarget::OnDragLeave(pWnd);

	if ( m_bUseDnDHelper )
	{
		m_piDropHelper->DragLeave();
	}
}

DROPEFFECT CNADroptarget::OnDragScroll(CWnd* pWnd, DWORD dwKeyState,CPoint point)
{
	if( m_lpDroptargeter ) {
		return m_lpDroptargeter->OnDragScroll(pWnd,dwKeyState,point);
	}else{
		return COleDropTarget::OnDragScroll(pWnd,dwKeyState,point);
	}
}


